part of YFree;

final yPages = <YPage>[];

enum YIntentType { Push, Present, Push_NoSlide, Push_Replace, Present_Replace, Push_NoSlide_Replace, Root_Replace }

extension YPage_Route on YPage {
  void yIntent(YPage page, {YIntentType intentType = YIntentType.Push, bool popToPage = false, bool reCreateWhenPopToPage = true}) {
    yRun(() {
      if (popToPage) {
        var hadPage = false;
        for (var item in yPages) {
          if (item.runtimeType == page.runtimeType) hadPage = true;
        }
        if (hadPage) {
          for (var i = yPages.length - 1; i >= 0; i--) {
            var curPage = yPages[i];
            if (curPage.runtimeType == page.runtimeType) {
              if (reCreateWhenPopToPage) {
                curPage.yFinish();
                break;
              } else {
                return;
              }
            } else {
              curPage.yFinish();
            }
          }
        }
      } else {
        for (var i = 0; i < yPages.length; i++) {
          if (yPages[i].runtimeType == page.runtimeType) return;
        }
      }
      yPages.add(page);
      switch (intentType) {
        case YIntentType.Root_Replace: // 替换
          Navigator.of(yPage.context).pushAndRemoveUntil(CupertinoPageRoute(builder: (context) => YStatefulWidget(() => page)), (route) => false);
          break;
        case YIntentType.Present: // 模态
          Navigator.push(
            yPage.context,
            PageRouteBuilder(
              transitionDuration: const Duration(milliseconds: 300),
              pageBuilder: (BuildContext context, Animation<double> animtion1, Animation<double> animtion2) => YStatefulWidget(() => page),
              transitionsBuilder: (BuildContext context, Animation<double> animtion1, Animation<double> animtion2, Widget widget) => SlideTransition(position: Tween<Offset>(begin: const Offset(0.0, 1.0), end: const Offset(0.0, 0.0)).animate(CurvedAnimation(parent: animtion1, curve: Curves.fastOutSlowIn)), child: widget),
            ),
          );
          break;
        case YIntentType.Push: // 压栈
          Navigator.push(yPage.context, CupertinoPageRoute(builder: (context) => YStatefulWidget(() => page)));
          break;
        case YIntentType.Push_NoSlide: // 压栈，无手势返回
          Navigator.push(
            yPage.context,
            PageRouteBuilder(
              transitionDuration: const Duration(milliseconds: 300),
              pageBuilder: (BuildContext context, Animation<double> animtion1, Animation<double> animtion2) => YStatefulWidget(() => page),
              transitionsBuilder: (BuildContext context, Animation<double> animtion1, Animation<double> animtion2, Widget widget) => SlideTransition(position: Tween<Offset>(begin: const Offset(1.0, 0.0), end: const Offset(0.0, 0.0)).animate(CurvedAnimation(parent: animtion1, curve: Curves.ease)), child: widget),
            ),
          );
          break;
        case YIntentType.Present_Replace: // 抽取替换，模态
          yFinish();
          Navigator.push(
            yPage.context,
            PageRouteBuilder(
              transitionDuration: const Duration(milliseconds: 300),
              pageBuilder: (BuildContext context, Animation<double> animtion1, Animation<double> animtion2) => YStatefulWidget(() => page),
              transitionsBuilder: (BuildContext context, Animation<double> animtion1, Animation<double> animtion2, Widget widget) => SlideTransition(position: Tween<Offset>(begin: const Offset(0.0, 1.0), end: const Offset(0.0, 0.0)).animate(CurvedAnimation(parent: animtion1, curve: Curves.fastOutSlowIn)), child: widget),
            ),
          );
          break;
        case YIntentType.Push_Replace: // 抽取替换，压栈
          yFinish();
          Navigator.push(yPage.context, CupertinoPageRoute(builder: (context) => YStatefulWidget(() => page)));
          break;
        case YIntentType.Push_NoSlide_Replace: // 抽取替换，压栈，无手势返回
          yFinish();
          Navigator.push(
            yPage.context,
            PageRouteBuilder(
              transitionDuration: const Duration(milliseconds: 300),
              pageBuilder: (BuildContext context, Animation<double> animtion1, Animation<double> animtion2) => YStatefulWidget(() => page),
              transitionsBuilder: (BuildContext context, Animation<double> animtion1, Animation<double> animtion2, Widget widget) => SlideTransition(position: Tween<Offset>(begin: const Offset(1.0, 0.0), end: const Offset(0.0, 0.0)).animate(CurvedAnimation(parent: animtion1, curve: Curves.ease)), child: widget),
            ),
          );
          break;
      }
    });
  }

  void yFinish({bool pop = true}) {
    for (var i = yPages.length - 1; i >= 0; i--) {
      if (yPages[i] == yPage) {
        yPages.removeAt(i);
        if (pop) {
          Navigator.pop(yPage.context);
          yBroadcast_unregisters();
        }
        break;
      }
    }
  }

  void yFinishToHome() {
    for (var i = yPages.length - 1; i > 0; i--) {
      yPages[i].yFinish();
    }
  }
}
